﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using PMKB_Web.pluginsCode;

namespace PMKB_Web.Controllers
{
     [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
    public class UploadController : Controller
    {
        //get show upload xap
        public ViewResult ShowUploadView()
        {
            ViewBag.XapUrl = Url.Content("~/Content/ClientBin/pkmb_client_bin_uploadfiles.xap");
            return View("ShowXap");
        }
        // post: /Upload/
        [HttpPost]
       

        public ActionResult UploadFile(string filename,int length )
        {
            var uploadfiledir = Server.MapPath("~/Files");
         //   HttpPostedFileBase file=Request.Files[0] as HttpPostedFileBase ;
            var file= Request.InputStream;
            try
            {
                if (file != null)
                {
                    var fil = new byte[length];
                    file.Read(fil, 0, length);
                    string name = Path.Combine(uploadfiledir, filename);
                    var newfile = new FileStream(name, FileMode.Create);
                    newfile.Write(fil, 0, length);
                    newfile.Close();
                    PluginControllerFactory.SavedFile(name);
                }
            }
                catch (IOException exp)
            {
                return new HttpStatusCodeResult(500, exp.Message);
            }
            return new HttpStatusCodeResult(200, "ok");
        }

    }
}
